<?php
/*
 * 企业库段落编辑
 * @package    company
 * @author     Mark
 * @copyright  Copyright (c) 2013 ixdcw team
 * @version    $Id: 13a68551032ab8dd904ce4c460562f0e0550d9ab $
 */

defined('IN_IXDCW') or exit('Access Denied');
require DT_CORE . '/' . $module . '/common.inc.php';
User::checkLogin(Env::uri());
$id = isset($id) ? intval($id) : 0;
if (empty($id)) {
	Prompt::message();
}

if ($submit)  {

	$oldCompanyInfo = CompanyEntry::getItemAndEdition($id);
	$ifMatchKey = $tid;
	$oldPattern = '<div class="hdwiki_tmml">';
	$oldItem =preg_split('/'.$oldPattern.'/',$oldCompanyInfo['content']);
	array_shift($oldItem);
	$oldItem = __($oldItem)->map(function($item) use ($oldPattern){
		return $oldPattern . $item;
	});

	unset($oldItem[$tid]);
	$oldItem[$tid] = $content;
	unset($content);
	foreach($oldItem as $val){
		$content .= $val;
	}


	$sql = Sql::insert(Table::name(Table::NAME_MERCHANT_EDITION), array(
		'did'             => $id,
		'content'         => $content,
		'summary'         => $summary,
		'company_thumb'   => $com_thumb,

		'company_type'    => $com_type,
		'company_size'    => $com_size,
		'company_year'    => $com_year,
		'company_unit'    => $com_unit,
		'company_money'   => $com_money,
		'company_areaid'  => $com_areaid,
		'company_address' => $com_address,
		'company_contact' => $com_contact,
		'company_telephone'=> $com_telephone,
		'company_mail'    => $com_mail,
		'company_business'=> $com_business,

		'reason'          => $reason,
		'author'          => $_username,
		'authorid'        => $_userid,
		'time'            => time(),
	));

	if (!$db->query($sql)) 	exit($db->error());
	$eid = $db->insertId();

	//添加类别
	CompanyEntry::addCategory($cates,$id,$eid,$_username,$_userid);

	Prompt::message('编辑成功, 请等待管理员审核！','list.php');

} else {

	if(CompanyAdmin::getEditionAudit($id) && $_groupid!=1)	Prompt::message('禁止编辑！','goback');
	$oldCompanyInfo = $companyInfo = CompanyEntry::getItemAndEdition($id);

	if (preg_match_all("/(<h1.*?>)(.*?)(<.+?>)/si", $companyInfo['content'], $matches)) {
		foreach($matches[1] as $v)	$str .= $v;
		preg_match_all("|<h1 id=\"cat(.*?)\" class=\"com-hd\">|", $str, $m);
		foreach($m[1] as $k => $v) $subtitles[$v] = trim($matches[2][$k]);
	}

	if(!$subtitles[$tid])  Prompt::message('编辑的段落不存在！','list.php');

	//类别
	if(!$catid = $category) $catid = CompanyEntry::getCatId($id);

	//获取类别数组
	$cates = $categoryArr = explode(',',$catid);

	//编辑器 选择的参数
	$parentid = CategoryModule::getParentid($categoryArr[0],PCache::read('category-25.php'));
	if($parentid==3834){
		$defaultDoc = array('发展历程','资本运作','运营体系','企业文化','产品品质','产品种类','配套供应','出口情况','品牌形象','发展战略','战略合作','研发实力','荣誉资质','质量认证','新闻事件',);
	}elseif($parentid==3835){
		$defaultDoc = array('发展历程','资本运作','运营体系','企业文化','商品品质','商品种类','配套供应','出口情况','质量认证','荣誉资质','发展战略','战略合作','新闻事件',	);
	}elseif($parentid==3836){
		$defaultDoc = array('发展历程','资本运作','运营体系','企业文化','综合实力','服务项目','质量认证','荣誉资质','发展战略','战略合作','物流线路','师资力量','新闻事件',	);
	}

	//根据ID 分解数据库中的数据
	if (isset($id) && $id != '') {
		$companyInfo = CompanyEntry::getItemAndEdition($id);
		extract($companyInfo);
		include template('editsection', 'company');
	}
}

